<?php
namespace App\Services\Enterprise;

use App\Model\Enterprise;
use App\Model\Position;
use App\Model\Tag;
use App\Services\CommonService;
use Illuminate\Support\Facades\DB;

class EnterpriseService extends CommonService
{
    //写完逻辑代码 调用数据库
    //判断传来的id是否正确
    public function sel($id){
        $arr = Enterprise::find($id)->toArray();
        //print_r($arr);die;
        //查出所有企业拥有的标签
        $res = Tag::where('reg_type',2)->whereIn('reg_id',json_decode($arr['enterprise_tap_id']))->get(['reg_id','reg_name']);
        //return json_decode($res);
        //添加一个新的key,存储对应的名称
        if (count(json_decode($arr['enterprise_tap_id']))>0){
            foreach ($res as $k => $v){
                $arr['reg_name'][] = json_decode($v)->reg_name;
            }
        }
        if ($arr!=[]){
            return $this->success($arr);
        }else{
            return $this->fail('301');
        }
    }
    /**
     * 执行修改
     */
    public function save($request){
        //接收企业id
        $id = $request->id;
        $file = $this->UploadImg($request);
        if ($request->hasFile('file')){
            //判断是否为上传图片
            $arr = [
                'enterprise_logo'=>$file,
            ];
        }else{
            //设置需要修改的数组
            $arr = [
                'enterprise_corporate_name'=>$request->enterprise_corporate_name,
                'enterprise_credit_code'=>$request->enterprise_credit_code,
                'enterprise_industry'=>$request->enterprise_industry,
                'enterprise_unit_size'=>$request->enterprise_unit_size,
                'enterprise_unit_address'=>$request->enterprise_unit_address,
                'enterprise_mailbox'=>$request->enterprise_mailbox,
                'enterprise_company_profile'=>$request->enterprise_company_profile,
                'enterprise_company_industry'=>$request->enterprise_company_industry,
                'enterprise_business_license'=>$request->enterprise_business_license
            ];
        }
        if($result = Enterprise::where('id','=',$id)->update($arr)==1){
            return $this->success($result);
        }else{
            return $this->fail('302','修改失败');
        }

    }
    /**
     * @param $request
     * 执行添加
     */
    public function add($request){
        /**
         * 判断是否传入视频
         */
        if ($request['enterprise_enterprise_video']==""){
            $request['enterprise_enterprise_video']=null;
        }
        return Enterprise::create(['enterprise_image'=>$request->enterprise_image,'enterprise_name'=>$request->enterprise_name,'enterprise_is_hot'=>$request->enterprise_is_hot,'enterprise_tap_id'=>$request->enterprise_tap_id,'enterprise_browse'=>$request->enterprise_browse,'enterprise_logo'=>$request->enterprise_logo,'enterprise_business_license'=>$request->enterprise_business_license,'enterprise_corporate_name'=>$request->enterprise_corporate_name,'enterprise_credit_code'=>$request->enterprise_credit_code,'enterprise_industry'=>$request->enterprise_industry,'enterprise_unit_size'=>$request->enterprise_unit_size,'enterprise_unit_address'=>$request->enterprise_unit_address,'enterprise_company_industry'=>$request->enterprise_company_industry,'enterprise_mailbox'=>$request->enterprise_mailbox,'enterprise_company_profile'=>$request->enterprise_company_profile,'enterprise_enterprise_video'=>$request->enterprise_enterprise_video]);
    }
    /**
     * 查询所有热门行业
     */
    public function selall(){
        //筛选出热门的8条数据
        $arr = Enterprise::where('enterprise_is_hot','=','1')->get(['id','enterprise_corporate_name','enterprise_logo'])->Take(8);
//        return $arr;
        //查询出所有的职位
        $res = Position::where('position_status','=','1')->get(['position_id','position_name','company_id'])->toArray();
//        return $res;
        foreach ($arr as $k => $v){
            foreach ($res as $kk => $vv){
                if ($v->id==$vv['company_id']){
                    $v->position_name .= " ".$vv['position_name'];
                    if (substr_count($v->position_name," ")>1){
                        break;
                    }
                }
            }
            $v->position_name = substr($v->position_name,1);
        }
        return $arr;
    }

    /**
     * 执行排序
     */
    public function order($request){
        //重新赋值
        $type = ($request->get('type')==1)?"desc":"asc";
        //查询所有的企业的数据
        $arr = Enterprise::orderBy('enterprise_unit_size',$type)->get(['id','enterprise_logo','enterprise_corporate_name','enterprise_tap_id']);
        //查出所有企业拥有的标签
        //添加一个新的key,存储对应的名称
        $res = Tag::where('reg_type',2)->get(['reg_id','reg_name'])->toArray();
        //return $res;
        foreach($arr as $k=>$v){
            foreach($res as $kk=>$vv){
                if(in_array($vv['reg_id'],json_decode($v->enterprise_tap_id))){
                    $arr[$k]->reg_name.=$vv['reg_name'].',';
                }
            }
            $arr[$k]->reg_name=substr($v->reg_name,0,-1);
        }
        return $this->success($arr);
    }

    /**
     * 修改视频路径
     */
    public function upload($request){
        //接收企业id
        $id = $request->id;
        $file = $this->UploadImg($request);
        //return $file;
        $arr = [
            'enterprise_enterprise_video'=>$file,
        ];
        $result = Enterprise::where('id','=',$id)->update($arr);
        return $this->success($result);
    }
}